package leecode

import "sort"

func findPeakElement(nums []int) int {
	if len(nums) <= 2 {
		//找到最大值的下标
		if len(nums) == 2 {
			if nums[0] > nums[1] {
				return 0
			} else {
				return 1
			}
		} else {
			return 0
		}
	}
	temp := make([]int, len(nums))
	copy(temp, nums)
	sort.Ints(temp)
	maxVal := temp[len(temp)-1]
	var index int
	for i := 1; i+1 < len(nums); i++ {
		if nums[i+1] < nums[i] && nums[i-1] < nums[i] {
			index = i
			break
		}
	}
	if index == 0 {
		for i := 0; i < len(nums); i++ {
			if nums[i] == maxVal {
				return i
			}
		}
	}
	return index
}
